<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Member;
use Illuminate\Http\Request;


class MemberController extends Controller
{
    // 个人中心 - 人员列表
    public function index()
    {
        $result = Member::whereUserId(auth('wechat')->id())
            ->paginate($this->limit);
        return $this->writeJson(0, $result->items());
    }

    // 新增/编辑人员
    public function add(Request $request)
    {
        if ($id = $request->post('id')) {
            $model = Member::whereUserId(auth('wechat')->id())
                ->where('id', $id)
                ->first();
            if (empty($model)) {
                return $this->writeJson(500, null, '数据不存在');
            }
        } else {
            $model = new Member();
            $model->user_id = auth('wechat')->id();
        }

        $model->group_id = $request->post('group_id');
        $model->username = $request->post('username');
        $model->mobile = $request->post('mobile');
        $model->height = $request->post('height');
        $model->weight = $request->post('weight');
        $model->age = $request->post('age');
        $model->sex = $request->post('sex');
        $model->id_card = $request->post('id_card');
        $model->introduce = $request->post('introduce');
        $model->save();
        return $this->writeJson(0,null,$id ? '修改成功' : '新增成功');
    }

    // 删除人员
    public function del(Request $request)
    {
        $id = $request->post('id');
        if(empty($id)){
            return $this->writeJson(500,null,'参数错误');
        }

        Member::whereUserId(auth('wechat')->id())
            ->where('id', $id)
            ->delete();
        return $this->writeJson(0,null,'删除成功');
    }
}
